﻿using System;
using System.Collections;
using Base;
using FSM;
using UnityEngine;

namespace Procedure
{
    /// <summary>
    /// 流程组件
    /// </summary>
    public class ProcedureComponent : GameFrameworkComponent
    {
        private IProcedureManager m_ProcedureManager = null;
        private ProcedureBase m_EntranceProcedure = null;
        
        [Header("可利用的流程类型名称")]
        [SerializeField] private string[] m_AvailableProcedureTypeNames = null;
        
        [Header("入口流程类型名称")]
        [SerializeField] private string m_EntranceProcedureTypeName = null;
        
        /// <summary>
        /// 当前流程
        /// </summary>
        public ProcedureBase CurrentProcedure
        {
            get
            {
                return m_ProcedureManager.CurrentProcedure;
            }
        }
        
        /// <summary>
        /// 当前流程时长
        /// </summary>
        public float CurrentProcedureDurationTime
        {
            get
            {
                return m_ProcedureManager.CurrentProcedureDurationTime;
            }
        }


        protected override void Awake()
        {
            base.Awake();
        
            // m_ProcedureManager = GameFrameworkEntry.GetModule<IProcedureManager>();
            if(m_ProcedureManager == null)
                return;
        }
        
        
        private IEnumerator Start()
        {
            ProcedureBase[] procedures = new ProcedureBase[m_AvailableProcedureTypeNames.Length];
            for (int i = 0; i < m_AvailableProcedureTypeNames.Length; i++)
            {
                //  从程序集中获取流程类型
                Type procedureType = Type.GetType(m_AvailableProcedureTypeNames[i]);
                if (procedureType == null)
                {
                    yield break;
                }
                //  实例化流程
                procedures[i] = (ProcedureBase)Activator.CreateInstance(procedureType);
                if(procedures[i] == null)
                    yield break;
                //  检查是否是入口流程
                if(m_EntranceProcedureTypeName == m_AvailableProcedureTypeNames[i])
                    m_EntranceProcedure = procedures[i];
            }

            if (m_EntranceProcedure == null)
                yield break;
            var m_FsmManager = new FsmManager();
            
            //  初始化流程管理器
            m_ProcedureManager.Initialize(m_FsmManager, procedures);
            
            yield return new WaitForEndOfFrame();
            //  开启入口流程
            m_ProcedureManager.StartProcedure(m_EntranceProcedure.GetType());
        }

        public bool HasProcedure<T>() where T : ProcedureBase
        {
            return m_ProcedureManager.HasProcedure<T>();
        }
        
        public bool HasProcedure(Type procedureType)
        {
            return m_ProcedureManager.HasProcedure(procedureType);
        }

        public ProcedureBase GetProcedure<T>() where T : ProcedureBase
        {
            return m_ProcedureManager.GetProcedure<T>();
        }
        
        public ProcedureBase GetProcedure(Type procedureType)
        {
            return m_ProcedureManager.GetProcedure(procedureType);
        }
    }

}